/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sim_test;

import java.awt.*;
import java.awt.Graphics;
import java.awt.geom.Line2D;
import java.awt.Dimension;
import javax.swing.*;

/**
 *
 * @author Administrator
 */
public class Sim_JPanelGrid extends JPanel
{

    private double widthOfSquare;
    private double heightOfSquare;
    private Sim_ControllerMain simControllerMain;

    public Sim_JPanelGrid(Sim_ControllerMain scm, Dimension size) {
        simControllerMain = scm;
        int[] intArrayOfNumberOfSquares = simControllerMain.getSimEngine().getWidthAndHeightOfSquares();
        widthOfSquare = intArrayOfNumberOfSquares[0];
        heightOfSquare = intArrayOfNumberOfSquares[1];
        
        setPreferredSize(size);
        setOpaque(false);
        //System.out.println(isOpaque());
        
        // clean up and error checking
        assert widthOfSquare == heightOfSquare : "width and height of the square in JpanelGrid does not match";
        intArrayOfNumberOfSquares = null;
    }

    @Override
    public void paintComponent(Graphics g) {
        double widthOfPanel = getWidth();
        double heightOfPanel = getHeight();
        
        Graphics2D g2 = (Graphics2D) g;

        int numberOfRows = (int) ((heightOfPanel / heightOfSquare) + 1); // Maybe Math.round?
        int numberOfColumns = (int) ((widthOfPanel / widthOfSquare + 1)); // Maybe Math.round?
        

        
        // draw the rows
        for (int i = 0,  yOfLine2D = 0; i < numberOfRows; i++, yOfLine2D += heightOfSquare) 
        {
            g2.draw(new Line2D.Double(0, (double) yOfLine2D, widthOfPanel,
                    (double) yOfLine2D));
        }

        // draw the columns
        for (int n = 0,  xOfLine2D = 0; n < numberOfColumns; n++, xOfLine2D += widthOfSquare) 
        {
            g2.draw(new Line2D.Double((double) xOfLine2D, 0, (double) xOfLine2D, heightOfPanel));
        }

    }
}
